package com.glory.brepda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.brepda.R;
import com.glory.brepda.adapter.AddCarrierDataAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.CarrierListEntity;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.SimpleEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FTTrackOutBiandaiSuccess2Activity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvBinName)
    TextView tvBinName;
    @BindView(R.id.tvGlide)
    TextView tvGlide;
    @BindView(R.id.tvQTY)
    TextView tvQTY;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etMaterielModel)
    EditText etMaterielModel;
    @BindView(R.id.rvMaterielModelList)
    RecyclerView rvMaterielModelList;
    @BindView(R.id.llBoxLayout)
    LinearLayout llBoxLayout;
    private List<SimpleEntity.MantissaLotBinData> mList;
    private String mLotID;
    private String mMANTISSAFLAG;
    private AddCarrierDataAdapter materielModeAdapter;
    private String mEqpid;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fttrack_out_biandai_success2;
    }

    @Override
    protected void initView() {
        llBoxLayout.setVisibility(View.GONE);
        Bundle bundle = getIntent().getExtras();
        SimpleEntity.MantissaLotBinData data = (SimpleEntity.MantissaLotBinData) bundle.getSerializable("SELECTORDATA");
        SimpleEntity.MantissaLotBinData data1 = (SimpleEntity.MantissaLotBinData) bundle.getSerializable("lotbindatalist");
        mEqpid = bundle.getString("EQPID", "");
        mList = new ArrayList<>();
        if (data != null){
            mList.add(data);
        }
        if (data1 != null){
            mList.add(data1);
        }
        mLotID = bundle.getString("LOTID");
        mMANTISSAFLAG = bundle.getString("isCheck");

        tvTitle.setText("正常结束-(管装出站)");
        setMainLayout(llMain);
        tvLot.setText(mLotID);
        tvBinName.setText(data.getBINNAME());
        tvGlide.setText(data.getBINGRADE());
        tvQTY.setText(String.valueOf(data.getMAINQTY()));
        initRecyclerView(rvMaterielModelList,this);
        materielModeAdapter = getCarrierAdapter();
        rvMaterielModelList.setAdapter(materielModeAdapter);
        addActivity(this);

        initEdit(etMaterielModel, s -> {
            if (materielModeAdapter.getData().contains(s)) {
                showToast("当前载具已添加到载具列表中", false);
                return;
            }

            materielModeAdapter.getData().add(s);
            materielModeAdapter.notifyDataSetChanged();
            etMaterielModel.setText("");
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (materielModeAdapter.getData().size() <= 0){
            FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "当前未绑定载具是否继续提交?", ()
                    -> submit("GuideTrackout","","",""), () -> {}).show(getSupportFragmentManager(),"NOT_CARRIER");
            return;
        }
        //todo:// /出站Dialog，补充
        submit("GuideTrackout","","","");
    }

    private void submit(String actiontype,String reelnum,String guide,String packagetype) {
        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                        (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            SimpleEntity body = response.body().getResponse().getBody();
                            if (actiontype.equals("NomalTrackout")){
                                showConfirmDialogFinishAll(body.getTRACKSHOWMSG().equals("") ?
                                        "出站成功,批次：" + body.getLOTLIST().get(0).getLOTID() : body.getTRACKSHOWMSG());
                            }else {
                                submit("NomalTrackout",String.valueOf(body.getREELNUM()),body.getGUIDE(),body.getPACKAGETYPE());
                            }
                         }


                    }

                    @Override
                    public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "LOTFTTRACKOUT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actiontype);
                map.addProperty("EQUIPMENTID", mEqpid);
                map.addProperty("SHOWMSG", "N");
//                map.addProperty("ACTIONTYPE", "NomalTrackout");
                if("NomalTrackout".equals(actiontype)){
                    map.addProperty("PACKAGETYPE", packagetype);
                    map.addProperty("MANTISSAFLAG", mMANTISSAFLAG);
                    map.addProperty("REELNUM", reelnum);
                    map.addProperty("GUIDE", guide);
                }
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(mLotID));

                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
                map.add("LOTBINDATALIST", new JsonParser().parse(new Gson().toJson(mList)).getAsJsonArray());
                List<CarrierListEntity> carrierList = new ArrayList<>();
                if (materielModeAdapter.getData().size() > 0){
                    for (String s : materielModeAdapter.getData()){
                        carrierList.add(new CarrierListEntity(s));
                    }
                }
                map.add("LOTMULTICARRIERLIST", new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
                NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTTrackOutBiandaiSuccess2Activity.this, map, callBack);
            }
        });
    }

}
